﻿using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Runtime;
using JoinBox.JoinBox_Code.WPF.多文档标签.View;

namespace JoinBox.WPF.多文档标签
{
    public class AutoDocs : IExtensionApplication
    {
        /// <summary>
        /// 这个是所有程序都使用的与cad线程通讯
        /// </summary>
        public static AcadIntermediary AcadIntermediary;
        /// <summary>
        /// 这个是cad与文档栏的WPF线程通讯
        /// </summary>
        public static Anchor JAnchor;

        //构造函数先执行且一次
        public AutoDocs()
        {
            AcadIntermediary = new AcadIntermediary();
            AcadIntermediary.Show();
        }

        //加载dll就启动多文档标签 
        //且可以通过命令反复调用
        public void Initialize()
        {
            if (JAnchor == null)
            {
                JAnchor = new Anchor(AcadIntermediary);
            }
            else
            {
                Terminate();
                JAnchor = new Anchor(AcadIntermediary);
            }
        }

        public void Terminate()
        {
            JAnchor?.Dispose();
            JAnchor = null;
        }

        //用命令来移除多文档标签
        [CommandMethod("+Duo", CommandFlags.Modal)]
        public void AddDou()
        {
            var ed = Acap.DocumentManager.MdiActiveDocument.Editor;
            Initialize();
            ed.WriteMessage("\n添加多文档标签");
        }

        //用命令来移除多文档标签
        [CommandMethod("-Duo", CommandFlags.Modal)]
        public void RemoveDou()
        {
            var ed = Acap.DocumentManager.MdiActiveDocument.Editor;
            Terminate();
            ed.WriteMessage("\n移除多文档标签");
        }
    }

}
